import { ifInviteSuc, memberInfo, groups, projectResponse, NormalResonse, ifChangeSuc   } from "./vo"
import ajax from "../../../modules/ajax";
import config from "../../../config.json"


export async function changeMem (projectId:number,username:string,role:number):Promise<ifChangeSuc>{
    try {
        let res = await ajax.ajax(config.requestUrl+"/gapi/project/updateRole", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                projectId:projectId,
                username:username,
                role:role
            })
        });
        if (!res.ok) {
            return Promise.reject("net error");
        }
        return res.json();
    } catch (err: any) {
        console.log(err);
    }
    return Promise.reject("unkonw errow");
}

export async function queryMem (projectId:number):Promise<memberInfo>{
    try {
        let res = await ajax.ajax(config.requestUrl+"/gapi/project/getTeamList", {
            method: "GET",
            headers: {
                "Content-Type": "application/json",
            },
            body: {
                projectId:projectId,
            }
        });
        if (!res.ok) {
            return Promise.reject("net error");
        }
        return res.json();
    } catch (err: any) {
        console.log(err);
    }
    return Promise.reject("unkonw errow");
}



export async function inviteMem (projectId:number, userName:string):Promise<ifInviteSuc>{
    try {
        let res = await ajax.ajax(config.requestUrl+"/gapi/message/send", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                projectId:projectId,
                username:userName,
            })
        });
        if (!res.ok) {
            return Promise.reject("net error");
        }
        return res.json();
    } catch (err: any) {
        console.log(err);
    }
    return Promise.reject("unkonw errow");
}


export async function getGroups(projectId: number):Promise<groups>{
    try {
        let res = await ajax.ajax(config.requestUrl+"/gapi/group/getList", {
            method: "GET",
            body: {
                projectId: projectId
            }
        });
        if (!res.ok) {
            return Promise.reject("错误状态码:" + res.statusCode);
        }
        return res.json();
    } catch (err: any) {
        console.log(err);
    }
    return Promise.reject("unkonw errow");
}

export async function getProject(projectId: number):Promise<projectResponse>{
    try {
        let res = await ajax.ajax(config.requestUrl+"/gapi/project/get", {
            method: "GET",
            body: {
                projectId: projectId
            }
        });
        if (!res.ok) {
            return Promise.reject("错误状态码:" + res.statusCode);
        }
        return res.json();
    } catch (err: any) {
        console.log(err);
    }
    return Promise.reject("unkonw errow");
}

export async function modProject(body: any):Promise<NormalResonse>{
    try {
        let res = await ajax.ajax(config.requestUrl+"/gapi/project/update", {
            method: "POST",
            body: JSON.stringify(body),
            headers: {
                "Content-Type": "application/json",
            },
        });
        if (!res.ok) {
            return Promise.reject("错误状态码:" + res.statusCode);
        }
        return res.json();
    } catch (err: any) {
        console.log(err);
    }
    return Promise.reject("unkonw errow");
}
